package com.example.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.lang.Contract;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/request2")
public class Request2Controller {
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + "=" + cookie.getValue());
            }
        }
        return "获取Cookie成功!";
    }

    @RequestMapping("/getCookie2")
    public String getCookie(@CookieValue("java") String val){
        return "获取Cookie成功, java: " + val;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        //设置Session
        HttpSession session = request.getSession();
        session.setAttribute("name","zhangsan");
        session.setAttribute("age",18);
        session.setAttribute("info",new Person("lisi",22,"men","111@163.com"));

        return "设置Session成功!";

    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        //获取Session
        HttpSession session = request.getSession(false);
        if (session != null) {
            System.out.println("name:" + session.getAttribute("name"));
            System.out.println("age:" + session.getAttribute("age"));
            System.out.println("info:" + session.getAttribute("info"));
        }
        return "获取Session成功,name:" + session.getAttribute("name");
    }

    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute("name") String name){
        return "获取Session成功,name:" + name;
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "userAgent" + ":" + userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return "userAgent" + ":" + userAgent;
    }

    @ResponseBody
    @RequestMapping("/returnPage")
    public String returnPage() {
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/returnHtml")
    public String returnHtml() {
        return "<h1>我是一级标题</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJson")
    public Person returnJson() {
        return new Person("lisi",18,"男","123@163.com");
    }

    @ResponseBody
    @RequestMapping("/returnJson2")
    public Map<String, String>  returnJson2() {
        Map<String,String> map = new HashMap<>();
        map.put("id","1");
        map.put("name","wangwu");
        return map;
    }

    @ResponseBody
    @RequestMapping("setStatus")
    public String setStatus(HttpServletResponse response) {
        response.setStatus(486);
        return "设置状态码成功!";
    }

    @RequestMapping("setContentType")
    public String setContentType() {
        return "{\"OK\":1}";
    }

    @RequestMapping(value = "setContentType2",produces = "application/json")
    public String setContentType2() {
        return "{\"OK\":1}";
    }

    @RequestMapping("setHeader")
    public String setHeader(HttpServletResponse response) {
        response.setHeader("My-Header","myHeader");
        return "Header设置成功";
    }
}
